package wh.汉明距离;

import wh.实现strStr函数.Str;

import java.util.Map;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/1/10 17:40
 * @description:
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        int i = hammingDistance(1, 4);
        System.out.println(i);
        String s = "00001";
    }

    public static int hammingDistance(int x, int y) {
            int ans=0;
            String xx=Integer.toBinaryString(x);
            String yy=Integer.toBinaryString(y);
            if (xx.length()>yy.length()){
                for (int i = 0; i < xx.length()-yy.length(); i++) {
                    if (xx.charAt(i)=='1')
                        ans++;
                }
                for (int i = 0; i < yy.length(); i++) {
                    if (xx.charAt(i+xx.length()-yy.length())!=yy.charAt(i))
                        ans++;
                }
            }else {
                for (int i = 0; i < yy.length()-xx.length(); i++) {
                    if (yy.charAt(i)=='1')
                        ans++;
                }
                for (int i = 0; i < xx.length(); i++) {
                    if (yy.charAt(i+yy.length()-xx.length())!=xx.charAt(i))
                        ans++;
                }

            }
            return ans;
    }


    public static int hammingDistance2(int x, int y) {
        //位异或运算（^）
        //运算规则是：两个数转为二进制，然后从高位开始比较，如果相同则为0，不相同则为1。
        return Integer.bitCount(x ^ y);
    }



}
